#include "tcpserver.h"
#include "ui_tcpserver.h"
#include "mytcpserver.h"

#include<QByteArray>
#include<QMessageBox>
#include<QHostAddress>

TcpServer::TcpServer(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::TcpServer)
{
    ui->setupUi(this);
    loadConfig();

    MyTcpServer::getInstance().listen(QHostAddress(m_ip), m_port);
}

TcpServer::~TcpServer()
{
    delete ui;
}

void TcpServer::loadConfig()
{
    QFile file(":/server.config");
    if(file.open(QIODeviceBase::ReadOnly))
    {
        QByteArray baData = file.readAll();
        QString strData = baData.toStdString().c_str();

        strData.replace("\r\n", " ");
        QStringList strDataList = strData.split(" ");
        m_ip = strDataList.at(0);
        m_port = strDataList.at(1).toUShort();
        qDebug()<<"ip:"<<m_ip<<" port:"<<m_port;

        file.close();
    }
    else
    {
        QMessageBox::critical(this, "open config", "open config fail");
    }
}
